<template>
  <!-- 我的日程 -->
  <div id="mySchedule">
    <div class="topBox">
      <div class="left">
        <img :src="require('/public/img/icon10.webp')" alt="" />
        <p class="name">我的日程</p>
      </div>
      <div class="right">
        <!-- <el-button
          type="primary"
          icon="el-icon-plus"
          circle
          @click="openDialog"
        ></el-button> -->
      </div>
    </div>
    <!-- <el-calendar v-model="date" ref="calendar">
      <template #header="{ date }">
        <span>{{ date }}</span>
        <el-button-group>
          <el-button size="small" @click="selectDate(date, 'prev-month')">
            上个月
          </el-button>
          <el-button size="small" @click="selectDate(date, 'today')"
            >今天</el-button
          >
          <el-button size="small" @click="selectDate(date, 'next-month')">
            下个月
          </el-button>
        </el-button-group>
      </template>
      <template #date-cell="{ data }">
        <div class="el-calendar-day" @click="getScheduleList(data.day)">
          <span>{{ data.day.slice(8) }}</span>
        </div>
      </template>
    </el-calendar> -->
    <!-- 日程列表 -->
    <div class="list" v-if="scheduleData.length != 0">
      <div class="item" v-for="(item, index) in scheduleData" :key="item.id">
        <div class="titleBox">日程 {{ index + 1 }}</div>
        <div class="detail">
          <div class="title">{{ item.title }}</div>
          <p class="content">{{ item.content }}</p>
          <p class="time">{{ item.startTime }} -- {{ item.endTime }}</p>
          <p class="value">参与人: {{ item.ownerAdminNames }}</p>
          <p class="value">地点：{{ item.address }}</p>
        </div>
      </div>
    </div>
    <el-empty :image-size="100" description="暂无日程" v-else></el-empty>
    <!-- 添加日程 -->
    <el-dialog
      v-model="dialogVisible"
      title="添加日程"
      width="50%"
      destroy-on-close
      :before-close="handleClose"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="dialogForm"
        label-width="auto"
        label-position="right"
      >
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="form.title"
            clearable
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="地点" prop="address">
          <el-input
            v-model="form.address"
            clearable
            placeholder="请输入地点"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content" style="width: 100%">
          <el-input
            v-model="form.content"
            clearable
            placeholder="请输入内容"
            type="textarea"
            rows="3"
          ></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="请选择计划回款日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            :disabled-date="disabledDate"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="请选择计划回款日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            :disabled-date="disabledDate"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="参与人" prop="ownerAdminList">
          <el-select
            v-model="form.ownerAdminList"
            placeholder="请选择参与人"
            style="width: 100%"
            clearable
            multiple
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否提醒" prop="eventType" style="width: 100%">
          <el-switch
            v-model="form.eventType"
            :inactive-value="0"
            :active-value="1"
          />
        </el-form-item>
        <el-form-item
          label="提醒时间"
          prop="remindType"
          v-if="form.eventType == 1"
        >
          <el-select
            v-model="form.remindType"
            placeholder="请选择提醒时间"
            style="width: 100%"
            clearable
          >
            <el-option
              v-for="item in remindTypeList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="add" :loading="isSaveing">
            保 存
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { queryTypeByDic } from '@/utils/getData'
export default {
  name: 'mySchedule',
  data () {
    return {
      // list: [],
      date: new Date(),
      scheduleData: [],
      dialogVisible: false,
      isSaveing: false,
      form: {
        createAdminId: '',
        title: '',
        content: '',
        address: '',
        startTime: '',
        endTime: '',
        ownerAdminList: [],
        ownerAdminIds: [],//参与人用户id
        eventType: 0,//提醒状态：0不提醒 1提醒
        remindType: 0,//提醒时间：0准时提醒1：5分钟前2：15分钟前3：30分钟前4：一个小时前5：两个小时前6：一天前7：两天前8：一周前
      },
      detailForm: {},
      userList: [],
      remindTypeList: [],//提醒时间类型
      userInfo: '',
      //验证规则
      rules: {
        title: [
          { required: true, message: '请输入标题', }
        ],
        content: [
          { required: true, message: '请输入内容', }
        ],
        address: [
          { required: true, message: '请输入地点', }
        ],
        startTime: [
          {
            type: 'date',
            required: true,
            message: '请选择开始时间',
            trigger: 'change',
          },
        ],
        endTime: [
          {
            type: 'date',
            required: true,
            message: '请选择结束时间',
            trigger: 'change',
          },
        ],
      }
    }
  },
  beforeMount () {
    this.userInfo = this.$TOOL.data.get('USER_INFO')
  },
  mounted () {
    // let day = new Date()
    // let year = day.getFullYear()
    // let month = +day.getMonth() + 1
    // let time = `${year}-${month}`
    this.getScheduleList(this.$TOOL.dateFormat(this.date, "yyyy-MM-dd"))
    this.getUserList()
    this.getType()
  },
  methods: {
    // 获取日程列表
    async getScheduleList (time) {
      console.log(time)
      let param = {
        adminId: this.userInfo.userId,
        time
      }
      let res = await this.$API.schedule.getScheduleForDay.get(param)
      console.log(res)
      this.scheduleData = res.data
    },

    // 获取用户
    async getUserList () {
      let param = {
        limit: 1000,
        page: 1
      }
      let { data } = await this.$API.user.getUserList.get(param)
      this.userList = data
    },
    // 获取提醒时间类型
    async getType () {
      let type = await queryTypeByDic('remindType')
      this.remindTypeList = type

    },

    openDialog () {
      let date = new Date()
      this.form.startTime = this.$TOOL.dateFormat(date, 'yyyy-MM-dd hh:mm:ss')
      this.dialogVisible = true
    },

    //添加日程
    async add () {
      this.form.createAdminId = this.userInfo.userId
      this.form.ownerAdminIds = this.form.ownerAdminList.join(',')
      let data = { ...this.form }
      delete data.ownerAdminList
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let res = await this.$API.schedule.addSchedule.post(data)
          this.isSaveing = false
          if (res.code == 200) {
            this.getScheduleList(this.$TOOL.dateFormat(this.date, 'yyyy-MM-dd'))
            this.$message.success("操作成功")
            this.handleClose()
          }
        }
      })
    },

    selectDate (date, val) {
      this.$refs.calendar.selectDate(val)
      let dateList = date.split(' ')
      let year = ''
      let month = ''
      let time = ''
      let day = new Date()
      switch (val) {
        case 'prev-month':
          year = dateList[0]
          month = +dateList[2] - 1
          if (month == 0) {
            month = 12
            year = +year - 1
          }
          time = `${year}-${month}`
          console.log(time)
          this.getScheduleList(time)
          break
        case 'today':
          year = day.getFullYear()
          month = +day.getMonth() + 1
          time = `${year}-${month}`
          this.getScheduleList(time)
          console.log(time)
          break
        case 'next-month':
          year = dateList[0]
          month = +dateList[2] + 1
          if (month == 13) {
            month = 1
            year = +year + 1
          }
          time = `${year}-${month}`
          this.getScheduleList(time)
          console.log(time)
          break
      }

    },

    // 关闭弹窗
    handleClose () {
      this.$refs.dialogForm.resetFields()
      this.dialogVisible = false
    },

    // 显示日程安排详情
    showdetailForm (item) {
      let nameList = []
      let idList = item.ownerAdminIds.split(',')
      this.userList.forEach(item => {
        idList.map(a_item => {
          if (item.id == +a_item) {
            nameList.push(item.username)
          }
        })
      })
      this.detailForm = item
      this.detailForm.ownerAdminNames = nameList.join(',')
    },
    // 设置就日期选择范围  今天之后
    disabledDate (time) {
      return time.getTime() < Date.now() - 8.64e7//如果没有后面的-8.64e7就是不可以选择
    }
  }
}
</script>
<style lang="scss" scoped>
#mySchedule {
  // margin-top: 20px;
  padding: 20px;
  background: #ffffff;
  border-radius: 8px;
  height: 100%;
  overflow: hidden;
  .topBox {
    display: flex;
    justify-content: space-between;
    padding-bottom: 20px;
    border-bottom: 1px #eee solid;
  }
  .left {
    display: flex;
    align-items: center;
    .name {
      margin-left: 10px;
      font-size: 16px;
    }
  }
  .right {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #999;
    cursor: pointer;
    .icon {
      margin-left: 5px;
      font-size: 16px;
    }
  }
  :deep(.el-calendar__header) {
    border-bottom: none;
  }
  :deep(.el-calendar__body) {
    padding: 20px;
    background: #edfaff;
  }
  :deep(.el-calendar-table__row) {
    height: 50px;
    .current,
    .next,
    .prev {
      position: relative;
      border: none;
      .el-calendar-day {
        position: absolute;
        left: 50%;
        top: 0;
        transform: translateX(-50%);
        width: 50px;
        height: 50px;
        border-radius: 50%;
      }
      span {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        font-size: 14px;
      }
    }
  }
  :deep(.is-selected) {
    background-color: transparent;
    .el-calendar-day {
      background: #366ef4;
      color: #fff;
    }
  }

  .list {
    margin-top: 20px;
    // padding-bottom: 20px;
    height: 440px;
    overflow-y: auto;

    .item {
      // margin-bottom: 20px
      margin: 20px;
    }
    .titleBox {
      width: 60px;
      padding: 5px 10px;
      text-align: center;
      background: #ecf2ff;
      border: 1px #baceff solid;
      color: #366ef4;
    }
    .detail {
      margin-top: 10px;
      padding-bottom: 10px;
      box-shadow: 0px 3px 9px 0px rgba(6, 39, 118, 0.15);
      p {
        margin: 15px 20px;
      }
      .title {
        padding: 8px;
        border-left: 3px #366ef4 solid;
        font-size: 14px;
      }
      .time {
        color: #366ef4;
      }
      .content {
        color: #666;
      }

      .value {
        color: #999;
      }
    }
  }
  .el-empty {
    height: 280px;
  }
}
</style>